package com.huawei.ivs.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.ivs.v2.IvsClient;
import com.huaweicloud.sdk.ivs.v2.model.DetectExtentionByIdCardImageRequest;
import com.huaweicloud.sdk.ivs.v2.model.DetectExtentionByIdCardImageResponse;
import com.huaweicloud.sdk.ivs.v2.model.DetectExtentionByNameAndIdRequest;
import com.huaweicloud.sdk.ivs.v2.model.DetectExtentionByNameAndIdResponse;
import com.huaweicloud.sdk.ivs.v2.model.DetectStandardByIdCardImageRequest;
import com.huaweicloud.sdk.ivs.v2.model.DetectStandardByIdCardImageResponse;
import com.huaweicloud.sdk.ivs.v2.model.DetectStandardByNameAndIdRequest;
import com.huaweicloud.sdk.ivs.v2.model.DetectStandardByNameAndIdResponse;
import com.huaweicloud.sdk.ivs.v2.model.ExtentionReqDataByIdCardImage;
import com.huaweicloud.sdk.ivs.v2.model.ExtentionReqDataByNameAndId;
import com.huaweicloud.sdk.ivs.v2.model.IvsExtentionByIdCardImageRequestBody;
import com.huaweicloud.sdk.ivs.v2.model.IvsExtentionByIdCardImageRequestBodyData;
import com.huaweicloud.sdk.ivs.v2.model.IvsExtentionByNameAndIdRequestBody;
import com.huaweicloud.sdk.ivs.v2.model.IvsExtentionByNameAndIdRequestBodyData;
import com.huaweicloud.sdk.ivs.v2.model.IvsStandardByIdCardImageRequestBody;
import com.huaweicloud.sdk.ivs.v2.model.IvsStandardByIdCardImageRequestBodyData;
import com.huaweicloud.sdk.ivs.v2.model.IvsStandardByNameAndIdRequestBody;
import com.huaweicloud.sdk.ivs.v2.model.IvsStandardByNameAndIdRequestBodyData;
import com.huaweicloud.sdk.ivs.v2.model.Meta;
import com.huaweicloud.sdk.ivs.v2.model.ReqDataByIdCardImage;
import com.huaweicloud.sdk.ivs.v2.model.StandardReqDataByNameAndId;
import com.huaweicloud.sdk.ivs.v2.region.IvsRegion;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Demo {

    private static final Logger LOGGER = LoggerFactory.getLogger(Demo.class.getName());

    // 认证人姓名
    private static final String VERIFICATION_ID = "张三";

    // 认证人身份证号码
    private static final String VERIFICATION_NAME = "612************11";

    public static String getImgStr(String imgPath) {
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            int read = in.read(data);
            LOGGER.debug(String.format("data read : %s", read));
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    LOGGER.error(e.getMessage());
                }
            }
        }
        return Base64.encodeBase64String(data);
    }

    public static void main(String[] args) {
        // 替换为真实的AK SK
        String ak = "Qa2**************QdV";
        String sk = "aBD**********************************cRv";

        // 获取resources下的图片路径
        URL faceFile = Demo.class.getClassLoader().getResource("data/face-demo.jpg");
        URL idCardFile = Demo.class.getClassLoader().getResource("data/idcard-demo.jpg");

        // 将图片转为base64
        String idcardImage1 = getImgStr(idCardFile.getPath());
        String idcardImage2 = getImgStr(idCardFile.getPath());
        String faceImage = getImgStr(faceFile.getPath());

        // Init Auth Info
        ICredential credential = getCredential(ak, sk);

        // create IvsClient
        IvsClient client = getClient(IvsRegion.CN_NORTH_4, credential);

        // 二要素身份证图片认证
        detectExtentionByIdCardImage(client, idcardImage1, idcardImage2);

        // 二要素姓名身份证认证
        detectExtentionByNameAndId(client);

        // 三要素人脸图片认证
        detectStandardByIdCardImage(client, idcardImage1, idcardImage2, faceImage);

        // 三要素姓名身份证认证
        detectStandardaByNameAndId(client, faceImage);
    }

    private static void detectStandardaByNameAndId(IvsClient client, String faceImage) {
        DetectStandardByNameAndIdRequest request = new DetectStandardByNameAndIdRequest();
        IvsStandardByNameAndIdRequestBody requestBody = new IvsStandardByNameAndIdRequestBody();
        Meta meta = new Meta();
        meta.setUuid("10eb0091-887f-4839-9929-cbc884f1e20e");
        requestBody.withMeta(meta);
        IvsStandardByNameAndIdRequestBodyData bodyData = new IvsStandardByNameAndIdRequestBodyData();
        List<StandardReqDataByNameAndId> byNameAndIds = new ArrayList<>();
        StandardReqDataByNameAndId byNameAndId = new StandardReqDataByNameAndId();
        byNameAndId.withFaceImage(faceImage);
        byNameAndId.withVerificationId(VERIFICATION_ID);
        byNameAndId.withVerificationName(VERIFICATION_NAME);
        byNameAndIds.add(byNameAndId);
        bodyData.withReqData(byNameAndIds);
        requestBody.withData(bodyData);
        request.withBody(requestBody);
        try {
            DetectStandardByNameAndIdResponse response = client.detectStandardByNameAndId(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.getErrorMsg());
        }
    }

    private static void detectStandardByIdCardImage(IvsClient client, String idcardImage1, String idcardImage2,
        String faceImage) {
        DetectStandardByIdCardImageRequest imageRequest = new DetectStandardByIdCardImageRequest();
        IvsStandardByIdCardImageRequestBody requestBody = new IvsStandardByIdCardImageRequestBody();
        Meta meta = new Meta();
        meta.setUuid("10eb0091-887f-4839-9929-cbc884f1e20e");
        requestBody.withMeta(meta);
        IvsStandardByIdCardImageRequestBodyData bodyData = new IvsStandardByIdCardImageRequestBodyData();
        List<ReqDataByIdCardImage> byIdCardImages = new ArrayList<>();
        ReqDataByIdCardImage idCardImage = new ReqDataByIdCardImage();
        idCardImage.withIdcardImage1(idcardImage1);
        idCardImage.withIdcardImage2(idcardImage2);
        idCardImage.withFaceImage(faceImage);
        byIdCardImages.add(idCardImage);
        bodyData.withReqData(byIdCardImages);
        requestBody.withData(bodyData);
        imageRequest.withBody(requestBody);
        try {
            DetectStandardByIdCardImageResponse response = client.detectStandardByIdCardImage(imageRequest);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.getErrorMsg());
        }
    }

    private static void detectExtentionByIdCardImage(IvsClient client, String idcardImage1, String idcardImage2) {
        DetectExtentionByIdCardImageRequest imageRequest = new DetectExtentionByIdCardImageRequest();
        IvsExtentionByIdCardImageRequestBody requestBody = new IvsExtentionByIdCardImageRequestBody();
        Meta meta = new Meta();
        meta.setUuid("10eb0091-887f-4839-9929-cbc884f1e20e");
        requestBody.withMeta(meta);
        IvsExtentionByIdCardImageRequestBodyData bodyData = new IvsExtentionByIdCardImageRequestBodyData();
        List<ExtentionReqDataByIdCardImage> idCardImages = new ArrayList<>();
        ExtentionReqDataByIdCardImage idCardImage = new ExtentionReqDataByIdCardImage();
        idCardImage.withIdcardImage1(idcardImage1);
        idCardImage.withIdcardImage2(idcardImage2);
        idCardImages.add(idCardImage);
        bodyData.withReqData(idCardImages);
        requestBody.withData(bodyData);
        imageRequest.withBody(requestBody);
        try {
            DetectExtentionByIdCardImageResponse response = client.detectExtentionByIdCardImage(imageRequest);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.getErrorMsg());
        }

    }

    private static void detectExtentionByNameAndId(IvsClient client) {
        DetectExtentionByNameAndIdRequest byNameAndIdRequest = new DetectExtentionByNameAndIdRequest();
        IvsExtentionByNameAndIdRequestBody requestBody = new IvsExtentionByNameAndIdRequestBody();
        IvsExtentionByNameAndIdRequestBodyData requestBodyData = new IvsExtentionByNameAndIdRequestBodyData();
        List<ExtentionReqDataByNameAndId> extentionReqDataByNameAndIds = new ArrayList<>();
        ExtentionReqDataByNameAndId byNameAndId = new ExtentionReqDataByNameAndId();
        byNameAndId.setVerificationId(VERIFICATION_ID);
        byNameAndId.setVerificationName(VERIFICATION_NAME);
        extentionReqDataByNameAndIds.add(byNameAndId);
        requestBodyData.withReqData(extentionReqDataByNameAndIds);
        requestBody.withData(requestBodyData);
        Meta meta = new Meta();
        meta.setUuid("uuid");
        requestBody.withMeta(meta);
        byNameAndIdRequest.setBody(requestBody);
        try {
            DetectExtentionByNameAndIdResponse response = client.detectExtentionByNameAndId(byNameAndIdRequest);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.getErrorMsg());
        }
    }

    public static ICredential getCredential(String ak, String sk) {
        return new BasicCredentials().withAk(ak).withSk(sk);
    }

    public static IvsClient getClient(Region region, ICredential auth) {
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        return IvsClient.newBuilder().withCredential(auth).withRegion(region).withHttpConfig(config).build();
    }

}
